package com.xsky.logic.service.impl;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.xsky.common.util.FilePathImpl;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.logic.ConfigureLogic;
import com.xsky.logic.DepartLogic;
import com.xsky.logic.service.facade.IXskyService;
import com.xsky.logic.user.UserLogic;

	public class XskyService implements IXskyService {
		
		//用户
		public String resetPwd(String userId) {
			return new UserLogic().resetPwd(userId);
		}
		public String changePassword(String id, String newPassword) {
			return new UserLogic().changePassword(id, newPassword);
		}
		public String logon(String userName,String password) {
			return new UserLogic().logon(userName, password);
		}
		
		public static int compare_date(String DATE1, String DATE2) {

			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			try 
			{
				Date dt1 = df.parse(DATE1);
				Date dt2 = df.parse(DATE2);
				if (dt1.getTime() > dt2.getTime()) {
					System.out.println("dt1 在dt2前");
					return 1;
				} else if (dt1.getTime() < dt2.getTime()) {
					System.out.println("dt1在dt2后");
					return -1;
				} else {
					return 0;
				}
			} 
			catch (Exception exception) 
			{
				exception.printStackTrace();
			}
			return 0;
		}

		public String deleteFile(String uri) {
			FilePathImpl filePathImpl = new FilePathImpl();
			String basePath = filePathImpl.getLocalPath();
			File file = new File(basePath + uri);
			try {
				if (file.isFile() && file.exists()) {

					file.delete();

				}

			} catch (Exception e) {
				return "文件删除失败，请确认文件未被打开";
			}
			return "文件成功删除";
		}
		
		public String genConfigure(String parentId) {
			String fullName = "com.xsky.operation.vo.TabConfigure";
			Class entityClass = null;
			try {
				entityClass = Class.forName(fullName);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
			List resultList = baseDao
					.findByHql("from TabConfigure model where model.parentId='"
							+ parentId + "' order by model.code");
			StringBuffer resultXml = new StringBuffer("");
			resultXml.append("<data>");
			XStream xs = new XStream();
			xs.alias("item", entityClass);
			// xs.registerConverter(new XStreamClobConverter());
			for (int i = 0; i < resultList.size(); i++) {
				resultXml.append(xs.toXML(resultList.get(i)));
			}
			resultXml.append("</data>");
			return resultXml.toString().replace(" 00:00:00.0", "");
		}
		public String searchById(String tableName, String key, String value) {
			// TODO Auto-generated method stub
			return null;
		}
		public String genSubByParentId(String parentId)
		{
			return new ConfigureLogic().genSubByParentId(parentId);
		}
	}
